/** 
 * Duplex：双工流，既可读又可写
*/
'use strict'
import { Duplex } from 'stream'
const duplex = Duplex()

// 可读端底层读取逻辑
duplex._read = function () {
  this._readNum = this._readNum || 0
  if (this._readNum > 1) {
    this.push(null)
  } else {
    this.push('' + (this._readNum++))
  }
}

// 可写端底层写逻辑
duplex._write = function (buf, enc, next) {
  // a, b
  process.stdout.write('_write ' + buf.toString() + '\n')
  next()
}

// 0, 1
duplex.on('data', buf=>{
  process.stdout.write(buf)
})
duplex.on('finish', ()=>{
  console.log('finish')
})
duplex.on('end', ()=>{
  console.log('end')
})
// duplex.pipe(process.stdout)

duplex.write('a')
duplex.write('b')
duplex.end()
// process.stdin.pipe(duplex)